<?php
class PhotoController extends Zend_Controller_Action {
	// Mảng tham số nhận được ở mỗi Action:
	protected $_arrParam;
	private $USER_ID = 0;
	private $URI = "/";
	private $USER_NAME = "yourtour";
	private $URL_ACC_LOGIN = "/account/login/";
	private $DEFAULT_PROFILE_PICTURE = "default/img/no_image.jpg";
	private $ALERT_TYPE_ERROR = "alert-error";
	private $ALERT_TYPE_SUCCESS = "alert-success";
	public function init() {
		$this->_arrParam = $this->_request->getParams ();
		// Truyền ra view
		$this->view->arrParam = $this->_arrParam;
		
		// Xu li cho phan header
		$this->URI = $this->getRequest ()->getScheme () . "://" . $this->getRequest ()->getHttpHost () . $this->getRequest ()->getRequestUri ();
		$mAccount = new Default_Model_Account ();
		if (! $mAccount->hasIdentity ()) {
			$this->_redirect ( $this->URL_ACC_LOGIN . '?return_url=' . $this->URI );
		} else {
			// get user info
			$this->USER_ID = $mAccount->getUserId ();
			$this->USER_NAME = $mAccount->getUserName ();
			
			$this->view->fullname = $mAccount->getFullName ();
			$this->view->return_url = $this->URI;
			$this->view->userid = $mAccount->getUserId ();
			$strAvatar = $mAccount->getProfilePicture ();
			if ($strAvatar != "") {
				$this->view->my_avatar = $mAccount->getProfilePicture ();
			} else {
				$this->view->my_avatar = $this->DEFAULT_PROFILE_PICTURE;
			}
			// get report
			$mRep = new Default_Model_Report ();
			$arrRep = $mRep->listAllReport ();
			$this->view->arrRep = $arrRep;
		}
		// End xu li cho phan header
	}
	public function indexAction() {
		$mImage = new Default_Model_Image ();
		$mPhoto = new Default_Model_Photo ();
		$mUser = new Default_Model_User ();
		$mLoc = new Default_Model_Location ();
		$mTour = new Default_Model_Tour ();
		$mUserTour = new Default_Model_UserTour ();
		
		if ($this->_request->isPost ()) {
			if (! empty ( $_FILES ['picture'] ['name'] ) && isset ( $_POST ['single'] )) {
				try {
					if ($mImage->uploadImg ( $this->USER_NAME )) {
						// insert to photo table
						$arrParam = array (
								'tour_id' => 0,
								'location_id' => 0,
								'photo_link' => $mImage->getImgName (),
								'user_id' => $this->USER_ID,
								'created_time' => date ( 'Y-m-d H:i:s' ) 
						);
						if (! $mPhoto->insertPhoto ( $arrParam )) {
							$this->view->display_alert = 1;
							$this->view->alert_type = $this->ALERT_TYPE_ERROR;
							$this->view->alert_message = '<strong>Error!</strong> ' . $mPhoto->getError ();
						}
					} else {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_ERROR;
						$this->view->alert_message = '<strong>Error!</strong> ' . $mImage->getError ();
					}
				} catch ( Exception $e ) {
					$this->view->display_alert = 1;
					$this->view->alert_type = $this->ALERT_TYPE_ERROR;
					$this->view->alert_message = '<strong>Error!</strong> ' . $e->getMessage ();
				}
			} else if (! empty ( $_FILES ['picture'] ['name'] ) && isset ( $_POST ['multi'] )) {
				try {
					$arrFileName = $mImage->uploadMulti ( $this->USER_NAME );
					
					if (count ( $arrFileName ) != 0) {
						// insert to photo table
						foreach ( $arrFileName as $file ) {
							$arrParam = array (
									'tour_id' => 0,
									'location_id' => 0,
									'photo_link' => 'images/' . $this->USER_NAME . '/' . date ( 'Y-m-d' ) . '/' . $file,
									'user_id' => $this->USER_ID,
									'created_time' => date ( 'Y-m-d H:i:s' ) 
							);
							if (! $mPhoto->insertPhoto ( $arrParam )) {
								$this->view->display_alert = 1;
								$this->view->alert_type = $this->ALERT_TYPE_ERROR;
								$this->view->alert_message = '<strong>Error!</strong> ' . $mPhoto->getError ();
							}
						}
					} else {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_ERROR;
						$this->view->alert_message = '<strong>Error!</strong> Không thể upload.';
					}
				} catch ( Exception $e ) {
					$this->view->display_alert = 1;
					$this->view->alert_type = $this->ALERT_TYPE_ERROR;
					$this->view->alert_message = '<strong>Error!</strong> ' . $e->getMessage ();
				}
			}
		} else if ($this->_request->isGet ()) {
			if (isset ( $_GET ['delete'] ) && $_GET ['delete'] == 1) {
				if (isset ( $_GET ['photo_id'] )) {
					// delete user
					$mProcess = new Default_Model_YourTourProcess ();
					$strMessReturn = $mProcess->deletePhoto ( $_GET ['photo_id'] );
					if ($strMessReturn == "") {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_SUCCESS;
						$this->view->alert_message = 'Xoá ảnh thành công!';
					} else {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_ERROR;
						$this->view->alert_message = '<strong>Error!</strong> ' . $strMessReturn;
					}
				} else {
					$this->view->display_alert = 1;
					$this->view->alert_type = "";
					$this->view->alert_message = '<strong>Warning!</strong> Thiếu thông tin để xoá.';
				}
			} else if (isset ( $_GET ['ctour'] ) && $_GET ['ctour'] == 1) {
				if (isset ( $_GET ['photo_id'] ) && isset ( $_GET ['tour_id'] )) {
					// update photo
					$arrParam = array (
							'tour_id' => $_GET ['tour_id']
					);
					if ($mPhoto->updatePhotoInfo( $arrParam, $_GET ['photo_id'] )) {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_SUCCESS;
						$this->view->alert_message = 'Sửa ảnh thành công!';
					} else {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_ERROR;
						$this->view->alert_message = '<strong>Error!</strong> ' . $mPhoto->getError ();
					}
				} else {
					$this->view->display_alert = 1;
					$this->view->alert_type = "";
					$this->view->alert_message = '<strong>Warning!</strong> Thiếu thông tin chỉnh sửa.';
				}
			} else if (isset ( $_GET ['cloc'] ) && $_GET ['cloc'] == 1) {
				if (isset ( $_GET ['photo_id'] ) && isset ( $_GET ['loc_id'] )) {
					// update photo
					$arrParam = array (
							'location_id' => $_GET ['loc_id']
					);
					if ($mPhoto->updatePhotoInfo( $arrParam, $_GET ['photo_id'] )) {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_SUCCESS;
						$this->view->alert_message = 'Sửa ảnh thành công!';
					} else {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_ERROR;
						$this->view->alert_message = '<strong>Error!</strong> ' . $mPhoto->getError ();
					}
				} else {
					$this->view->display_alert = 1;
					$this->view->alert_type = "";
					$this->view->alert_message = '<strong>Warning!</strong> Thiếu thông tin chỉnh sửa.';
				}
			}
		}
		
		// get my idle photo
		$this->view->total_my_photo = $mPhoto->countMyIdlePhoto ( $this->USER_ID );
		$arrMyIdlePhoto = $mPhoto->getMyIdlePhoto ( $this->USER_ID );
		if (count ( $arrMyIdlePhoto ) != 0) {
			// paging
			$adapter = new Zend_Paginator_Adapter_DbSelect ( $arrMyIdlePhoto );
			$paginator = new Zend_Paginator ( $adapter );
			$paginator->setItemCountPerPage ( 12 );
			$paginator->setPageRange ( 3 );
			$currentMPage = $this->_request->getParam ( 'mpage', 1 );
			$paginator->setCurrentPageNumber ( $currentMPage );
			$this->view->arrMyIdlePhoto = $paginator;
		}
		
		// get my tour
		$arrMyTour = $mUserTour->getUserTour ( $this->USER_ID );
		if (count ( $arrMyTour ) != 0) {
			$arrMyTourInfo = null;
			foreach ( $arrMyTour as $mt ) {
				$arrMyTourInfo [] = $mTour->getTourInfoById ( $mt ['tour_id'] );
			}
			$this->view->arrMyTourInfo = $arrMyTourInfo;
		}
		
		// get all location
		$arrMyLoc = $mLoc->listAllLocationNonPaging();
		if (count ( $arrMyLoc ) != 0) {
			$this->view->arrMyLoc = $arrMyLoc;
		}
		
		// get all photo
		$this->view->total_photo = $mPhoto->countPhoto ();
		$arrPhoto = $mPhoto->listAllPhoto ();
		if (count ( $arrPhoto ) != 0) {
			// paging
			$adapter = new Zend_Paginator_Adapter_DbSelect ( $arrPhoto );
			$paginator = new Zend_Paginator ( $adapter );
			$paginator->setItemCountPerPage ( 99 );
			$paginator->setPageRange ( 3 );
			$currentPage = $this->_request->getParam ( 'page', 1 );
			$paginator->setCurrentPageNumber ( $currentPage );
			$this->view->arrPhoto = $paginator;
			
			// get more info
			$arrUser = null;
			$arrLoc = null;
			$arrTour = null;
			foreach ( $paginator as $photo ) {
				$arrUser [] = $mUser->getUserInfoById ( $photo ['user_id'] );
				$arrLoc [] = $mLoc->getLocationInfoById ( $photo ['location_id'] );
				$arrTour [] = $mTour->getTourInfoById ( $photo ['tour_id'] );
			}
			
			$this->view->arrUser = $arrUser;
			$this->view->arrLoc = $arrLoc;
			$this->view->arrTour = $arrTour;
		}
	}
}